package com.powerunion.datacollection.service;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import com.powerunion.datacollection.common.SystemException;
import com.powerunion.datacollection.dao.DaoFactory;
import com.powerunion.datacollection.dao.ItemCategoryDao;
import com.powerunion.datacollection.model.ItemCategory;
import com.powerunion.util.log.LogHelper;

public class ItemCategoryManager {
	private static Logger logger = LogHelper.getInstance().getLogger(ItemCategoryManager.class);
	private ItemCategoryDao dao = null;
	public ItemCategoryManager() {
		super();
		this.dao = (ItemCategoryDao)DaoFactory.getInstance().getDao(ItemCategoryDao.class);
	}
	
	public String save(String saveStr,int itemType) throws Exception {
		//参数为空
		if(StringUtils.isEmpty(saveStr)) {
			String[] args = { "saveStr" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter saveStr NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		
		JSONObject jsonObject = JSONObject.fromObject(saveStr);
		ItemCategory tc = (ItemCategory)JSONObject.toBean(jsonObject,ItemCategory.class);

		return this.dao.save(tc,itemType);
		
	}
	
	public boolean delete(String[] codes) throws Exception{
		//参数为空
		if(codes == null) {
			String[] args = { "codes" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter codes NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		
		return this.dao.delete(codes);
	}
	
	public ItemCategory getBykey(String id) throws Exception{
		//参数为空
		if(StringUtils.isEmpty(id)) {
			String[] args = { "id" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter id NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		
		return this.dao.getBykey(id);
	}
	
	public JSONArray getTree(String paraSeq,int categoryType,String userCode,int permissionType) throws Exception {
		//参数为空 
		if(StringUtils.isEmpty(paraSeq)) {
			String[] args = { "paraSeq" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter paraSeq NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		
		//参数为空 
		if(StringUtils.isEmpty(userCode)) {
			String[] args = { "userCode" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter userCode NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		
		return this.dao.getTree(paraSeq, categoryType, userCode, permissionType);
	}
	
	
}
